<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="array" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="PageViews" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.pageViews }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Display_time" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.displayTime }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      listLoading: true
    }
  },
  computed: {
    // Vuex状态管理【材料】获取方式一
    ...mapState({ array: (state) => state.table.array }),
    // Vuex状态管理【加工】获取方式二
    ...mapGetters(['array2'])
  },
  mounted() {
    this.fetchTableData()
  },
  methods: {
    fetchTableData() {
      this.listLoading = true
      // 远程API接口调用方式一：直接引入调用其API接口。跳过VueX状态管理存储。
      // getList().then(response => { this.list = response.data.items; this.listLoading = false })
      // 远程API接口调用方式二：通过Vuex状态管理存储。
      this.$store.dispatch('getTableList')
      this.listLoading = false
      // 材料
      console.log('Array1->', this.array)
      // 加工
      console.log('Array2->', this.array2)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
